package com.bra.view;

import com.bra.R;
import com.bra.utils.CityDate;
import com.hbyc.citywhellview.widget.CityPicker;
import com.hbyc.citywhellview.widget.OnValueChangedListener;
import com.hbyc.citywhellview.widget.adapters.ArrayPickerAdapter;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

/**
 * 选择城市三级联动的dialog
 * 
 * @author WZ
 * */
public class CityPickerDialog extends Dialog implements
		android.view.View.OnClickListener, OnValueChangedListener {
	private Activity activity;
	private TextView dismiss;
	private TextView save;
	/**
	 * 省
	 * */
	private CityPicker province_picker;
	/**
	 * 市
	 * */
	private CityPicker city_picker;
	/**
	 * 区
	 * */
	private CityPicker area_picker;
	private TextView textView;

	public CityPickerDialog(Activity activity, int theme,
			final TextView textView) {
		super(activity, theme);
		// TODO 自动生成的构造函数存根
		this.activity = activity;
		this.textView = textView;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.radius_diaog_citys);
		findViewById();
		setUpData();
	}

	private void setUpData() {
		CityDate.initProvinceDatas(activity);
		province_picker.setViewAdapter(new ArrayPickerAdapter<String>(activity,
				CityDate.getProvinceArray()));
		// 设置可见条目数量
		province_picker.setVisibleItems(7);
		city_picker.setVisibleItems(7);
		area_picker.setVisibleItems(7);
		updateCities();
		updateAreas();
	}

	private void findViewById() {
		dismiss = (TextView) findViewById(R.id.city_dismiss);
		save = (TextView) findViewById(R.id.city_save);
		province_picker = (CityPicker) findViewById(R.id.province_picker);
		province_picker.setShadowColor(0xefd0d0d0, 0xcfE9E9E9, 0x3ff6f6f6);
		province_picker.setPickerBackground(R.drawable.city_picker_bg);
		province_picker.setPickerForeground(R.drawable.city_picker_val);
		province_picker.setPickerLine(R.color.light_gray);
		city_picker = (CityPicker) findViewById(R.id.city_picker);
		city_picker.setShadowColor(0xefd0d0d0, 0xcfE9E9E9, 0x3ff6f6f6);
		city_picker.setPickerBackground(R.drawable.city_picker_bg);
		city_picker.setPickerForeground(R.drawable.city_picker_val);
		city_picker.setPickerLine(R.color.light_gray);
		area_picker = (CityPicker) findViewById(R.id.area_picker);
		area_picker.setShadowColor(0xefd0d0d0, 0xcfE9E9E9, 0x3ff6f6f6);
		area_picker.setPickerBackground(R.drawable.city_picker_bg);
		area_picker.setPickerForeground(R.drawable.city_picker_val);
		area_picker.setPickerLine(R.color.light_gray);
		dismiss.setOnClickListener(this);
		province_picker.addChangingListener(this);
		city_picker.addChangingListener(this);
		area_picker.addChangingListener(this);
		save.setOnClickListener(this);
	}

	/**
	 * 根据当前的省，更新市WheelView的信息
	 */
	private void updateCities() {
		int pCurrent = province_picker.getCurrentItem();
		city_picker.setViewAdapter(new ArrayPickerAdapter<String>(activity,
				CityDate.getCityArray(pCurrent)));
		city_picker.setCurrentItem(0);
		updateAreas();
	}

	/**
	 * 根据当前的市，更新区WheelView的信息
	 */
	private void updateAreas() {
		int pCurrent = city_picker.getCurrentItem();
		area_picker.setViewAdapter(new ArrayPickerAdapter<String>(activity,
				CityDate.getDistricArray(pCurrent)));
		area_picker.setCurrentItem(0);
	}

	@Override
	public void onClick(View v) {
		// TODO 自动生成的方法存根
		switch (v.getId()) {
		case R.id.city_dismiss:
			this.dismiss();
			break;
		case R.id.city_save:
			this.dismiss();
			textView.setText(CityDate.getProvince(province_picker
					.getCurrentItem()).area_name
					+ ","
					+ CityDate.getCity(city_picker.getCurrentItem()).area_name
					+ ","
					+ CityDate.getDistric(area_picker.getCurrentItem()).area_name);
			break;
		default:
			break;
		}
	}

	/**
	 * 省选中条目
	 * */
	public int getProvinceCurrentItem() {
		return province_picker.getCurrentItem();
	}

	/**
	 * 市选中条目
	 * */
	public int getCityCurrentItem() {
		return city_picker.getCurrentItem();
	}

	/**
	 * 区选中条目
	 * */
	public int getDistricCurrentItem() {
		return area_picker.getCurrentItem();
	}

	@Override
	public void onChanged(CityPicker picker, int oldValue, int newValue) {
		// TODO Auto-generated method stub
		if (picker == province_picker) {
			updateCities();
		} else if (picker == city_picker) {
			updateAreas();
		} else if (picker == area_picker) {

		}
	}

}
